<?php


namespace App\Utils;

use \App\Models\Config;

class Tools
{
    use Singleton;

    /**
     * 获取中文首字母
     * @param $string
     * @return string
     */
    public function firstLetter($string){
        if (empty($string)) {
            return '';
        }
        $character = ord($string[0]);
        if ($character >= ord('A') && $character <= ord('z')) return strtoupper($string[0]);
        $s1 = iconv('UTF-8', 'gb2312', $string);
        $s2 = iconv('gb2312', 'UTF-8', $s1);
        $s = $s2 == $string ? $s1 : $string;
        $asc = ord($s[0]) * 256 + ord($s[1]) - 65536;
        if ($asc >= -20319 && $asc <= -20284) return 'A';
        if ($asc >= -20283 && $asc <= -19776 || $asc == -9743) return 'B';
        if ($asc >= -19775 && $asc <= -19219) return 'C';
        if ($asc >= -19218 && $asc <= -18711 || $asc == -9767) return 'D';
        if ($asc >= -18710 && $asc <= -18527) return 'E';
        if ($asc >= -18526 && $asc <= -18240) return 'F';
        if ($asc >= -18239 && $asc <= -17923) return 'G';
        if ($asc >= -17922 && $asc <= -17418) return 'H';
        if ($asc >= -17417 && $asc <= -16475) return 'J';
        if ($asc >= -16474 && $asc <= -16213) return 'K';
        if ($asc >= -16212 && $asc <= -15641 || $asc == -7182 || $asc == -6928) return 'L';
        if ($asc >= -15640 && $asc <= -15166) return 'M';
        if ($asc >= -15165 && $asc <= -14923) return 'N';
        if ($asc >= -14922 && $asc <= -14915) return 'O';
        if ($asc >= -14914 && $asc <= -14631 || $asc == -6745) return 'P';
        if ($asc >= -14630 && $asc <= -14150 || $asc == -7703) return 'Q';
        if ($asc >= -14149 && $asc <= -14091) return 'R';
        if ($asc >= -14090 && $asc <= -13319) return 'S';
        if ($asc >= -13318 && $asc <= -12839) return 'T';
        if ($asc >= -12838 && $asc <= -12557) return 'W';
        if ($asc >= -12556 && $asc <= -11848) return 'X';
        if ($asc >= -11847 && $asc <= -11056) return 'Y';
        if ($asc >= -11055 && $asc <= -10247) return 'Z';
        return '';
    }

    /**
     * 生产流水号
     * @param null $prefix
     * @param null $md5
     * @return string
     */
    public function serialNo($prefix = null, $md5 = null)
    {
        $serialNo = $prefix . (strtotime(date('YmdHis', time()))) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
        if ($md5) {
            return $md5 == 16 ? substr(md5($serialNo), 8, 16) : md5($serialNo);
        } else {
            return $serialNo;
        }
    }

    /**
     * MD5加密
     * @param $data
     * @param int $length
     * @return false|string|null
     */
    public function md5($data, $length = 16) {
        if (!$data) {
            return null;
        } elseif (!is_string($data)) {
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        }
        return $length == 16 ? substr(md5($data), 8, 16) : md5($data);
    }

    /**
     * 批量数值元素判断
     * @param $params
     * @param $data
     * @return false|string|null
     */
    public function keys_exists(array $params, array $data) {
        foreach($params as $key => $value) {
            if (in_array($key, $data)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 生成安全JSON数据
     * @param array $array
     * @return string
     */
    static public function json_encode($array)
    {
        return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function ($matches) {
            return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");
        }, json_encode($array));
    }
    
    static public function break_string($str, $num, $jg = 'str')
    {
        preg_match_all("/./u", $str, $arr); //将所有字符转成单个数组
        //print_r($arr);
        $strstr = '';
        $width = 0;
        $arr = $arr[0];
        $atr = array();
        foreach ($arr as $key => $string) {
            $strlen = strlen($string); //计算当前字符的长度，一个字母的长度为1，一个汉字的长度为3
            //echo $strlen;

            if ($strlen == 3) {

                $width += 1;
            } else {

                $width += 0.5;
            }
            $strstr .= $string;
            array_push($atr,$string);
            //计算当前字符的下一个
            if (array_key_exists($key + 1, $arr)) {
                $_strlen = strlen($arr[$key + 1]);
                if ($_strlen == 3) {
                    $_width = 1;
                } else {
                    $_width = 0.5;
                }
                if ($width + $_width > $num) {
                    $width = 0;
                    $strstr .= "\n";
                }
            }
        }
        if ($jg == 'str') {
            return $strstr;
        } else {
            return $atr;
        }
    }
}
